package com.gexiaobao.pigeon.ui.fragment.mine.banking

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.countDownCoroutines
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.param.ApplyBindCardParam
import com.gexiaobao.pigeon.app.model.param.SureBindCardParam
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentAddBankCardBinding
import com.gexiaobao.pigeon.viewmodel.MineBankingViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 19:43
 * @Describe    : 添加银行卡
 */
class FragmentAddBankCard : BaseFragment<MineBankingViewModel, FragmentAddBankCardBinding>() {

    private var tranceNum = ""

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.toolbar.initClose("添加银行卡") {
            nav().navigateUp()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvVerifyCodeSend, mDatabind.tvAccountBindCard) {
            when (it) {
                mDatabind.tvVerifyCodeSend -> {
                    sendVerifyCode()
                }

                mDatabind.tvAccountBindCard -> {
                    bindAccountCard()
                }
            }
        }
    }

    private fun bindAccountCard() {
        when {
            mDatabind.etInputCardNum.text?.isEmpty() == true -> showMessage("请输入银行卡号")
            mDatabind.etInputPhoneNum.text?.isEmpty() == true -> showMessage(getString(R.string.user_mobile_number))
            !Util.isTelPhoneNumber(mDatabind.etInputPhoneNum.text.toString()) -> showMessage(
                resources.getString(
                    R.string.user_phone_number_wrong
                )
            )

            mDatabind.etInputVerifyCode.text?.isEmpty() == true -> showMessage(getString(R.string.user_verification_code))
            else -> {
                val body = SureBindCardParam(
                    verificationCode = mDatabind.etInputVerifyCode.text.toString(),
                    phone = mDatabind.etInputPhoneNum.text.toString(),
                    tranceNum = tranceNum
                )
                showLoading(getString(R.string.loading))
                mViewModel.bindBankCard(body)
            }
        }
    }

    private fun sendVerifyCode() {
        if (mDatabind.etInputPhoneNum.text?.isEmpty() == true) {
            showMessage("手机号不可为空")
            return
        }
        if (!Util.isTelPhoneNumber(mDatabind.etInputPhoneNum.text.toString())) {
            showMessage(resources.getString(R.string.user_phone_number_wrong))
            return
        }

        val paramsBody = ApplyBindCardParam(
            carNo = mDatabind.etInputCardNum.text.toString(),
            phone = mDatabind.etInputPhoneNum.text.toString()
        )
        showLoading("发送中...")
        mViewModel.applyBindBankCardV2(paramsBody)
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()

        mViewModel.sureBindCardResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                RxToast.showToast("操作成功")
                setFragmentResult("ok", Bundle().apply {
                    putBoolean("isSuccess", true)
                })
                nav().navigateUp()
            } else {
                RxToast.showToast(it.errorMsg)
            }
        }

        mViewModel.bindCardResult.observe(viewLifecycleOwner) { resultState ->
            dismissLoading()
            parseState(resultState, { it ->
                job = countDownCoroutines(60, {
                    mDatabind.tvVerifyCodeSend.text =
                        "(" + it + ")" + getString(R.string.mine_account_unbind_phone_activity_later_second_reload)
                    mDatabind.tvVerifyCodeSend.isEnabled = false
                }, {
                    mDatabind.tvVerifyCodeSend.text = "获取验证码"
                    mDatabind.tvVerifyCodeSend.isEnabled = true
                }, lifecycleScope)
                tranceNum = it.tranceNum
                RxToast.showToast("发送成功")
            }, {
                RxToast.showToast(it.msg)
            })
        }
    }
}